﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace PowerManager.Settings
{
    public sealed partial class Settings : Form
    {
        private readonly IMainInterface _inter;
        private readonly IRegistryHelper _reg;
        public Settings(IMainInterface inter)
        {
            _inter = inter;
            _reg = new RegistryHelper();
            InitializeComponent();
            MinimumSize = MaximumSize = Size;
            LoadSettings();
            Icon = Properties.Resources.IconPower;
            chkAutorun.Checked = _reg.IsStartupItem(Properties.Resources.AppName);
            chkAutorun.CheckedChanged += ChkAutorunOnCheckedChanged;
            ChkAutorunOnCheckedChanged(null, EventArgs.Empty);
            SetToolTip();
        }

        private void ChkAutorunOnCheckedChanged(object sender, EventArgs eventArgs)
        {
            chkAutorun.ForeColor = chkAutorun.Checked ? Color.Blue : Color.Red;
        }

        void SetToolTip()
        {
            btnHint.ToolTipIcon = ToolTipIcon.Info;
            btnHint.ToolTipTitle = Properties.Resources.HintTitle;
            btnHint.SetToolTip(tbxSet, Properties.Resources.HintActive);
            btnHint.SetToolTip(tbxMComm, Properties.Resources.HintMComm);
            btnHint.IsBalloon = true;
        }

        void LoadSettings(bool loadDefault = false)
        {
            tbxActive.Text = loadDefault
                ? Properties.Resources.CmdGetActiveScheme
                : Properties.Settings.Default.CmdGetActiveScheme;
            tbxList.Text = loadDefault
                ? Properties.Resources.CmdGetListSchemes
                : Properties.Settings.Default.CmdGetListSchemes;
            tbxMComm.Text = loadDefault
                ? Properties.Resources.mCommand
                : Properties.Settings.Default.mCommand;
            tbxSet.Text = loadDefault
                ? Properties.Resources.CmdSetPowerScheme
                : Properties.Settings.Default.CmdSetPowerScheme;
            chkStart.Checked = loadDefault || Properties.Settings.Default.ShowStartMessage;

            lblColor.BackColor = loadDefault
                ? Color.FromName(Properties.Resources.ActiveSchemeColor)
                : Properties.Settings.Default.ActiveSchemeColor;
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Properties.Resources.DefaultConfirm,
                Properties.Resources.DefaultConfirmTitle,
    MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                return;

            LoadSettings(true);
            Properties.Settings.Default.Save();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(Properties.Resources.SaveConfirm, Properties.Resources.SaveConfirmTitle,
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                return;

            Properties.Settings.Default.CmdGetActiveScheme = tbxActive.Text.Trim();
            Properties.Settings.Default.CmdGetListSchemes = tbxList.Text.Trim();
            Properties.Settings.Default.CmdSetPowerScheme = tbxSet.Text.Trim();
            Properties.Settings.Default.mCommand = tbxMComm.Text.Trim();
            Properties.Settings.Default.ShowStartMessage = chkStart.Checked;
            Properties.Settings.Default.ActiveSchemeColor = lblColor.BackColor;
            Properties.Settings.Default.Save();
            Close();
        }

        private void chkAutorun_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (chkAutorun.Checked)
                {
                    _reg.AddToAutoRun(Properties.Resources.AppName);
                }
                else
                {
                    _reg.DelFromAutoRun(Properties.Resources.AppName);
                }
            }
            catch (Exception ex)
            {
                _inter.ErrorThrowed(string.Format("{0}\r\n{1}",
                    Properties.Resources.RegistryAccessError, ex.Message));
            }
        }

        private void lblColor_Click(object sender, EventArgs e)
        {
            if (colorPicker.ShowDialog() == DialogResult.OK)
            {
                lblColor.BackColor = colorPicker.Color;
            }
        }
    }
}
